using Microsoft.AspNetCore.Mvc;
using GroupSix.Application.Services;
using GroupSix.Application.Dtos;
using GroupSix.Api.Attributes;

namespace GroupSix.Api.Controllers;

/// <summary>
/// 菜单管理控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
// [RequirePermission("menu")] // 暂时注释掉认证要求，用于测试
public class MenuController : ControllerBase
{
    private readonly MenuService _menuService;

    public MenuController(MenuService menuService)
    {
        _menuService = menuService;
    }

    /// <summary>
    /// 创建菜单
    /// </summary>
    /// <param name="request">创建菜单请求</param>
    /// <returns>创建结果</returns>
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateMenuRequestDto request)
    {
        if (!ModelState.IsValid)
        {
            var errors = ModelState.Values
                .SelectMany(v => v.Errors)
                .Select(e => e.ErrorMessage)
                .ToList();
            return BadRequest(new { success = false, message = "请求参数验证失败", errors });
        }

        var (success, message, data) = await _menuService.CreateAsync(request);

        if (success)
        {
            return Ok(new { success = true, message, data });
        }
        else
        {
            return BadRequest(new { success = false, message });
        }
    }

    /// <summary>
    /// 更新菜单
    /// </summary>
    /// <param name="request">更新菜单请求</param>
    /// <returns>更新结果</returns>
    [HttpPut]
    public async Task<IActionResult> Update([FromBody] UpdateMenuRequestDto request)
    {
        if (!ModelState.IsValid)
        {
            var errors = ModelState.Values
                .SelectMany(v => v.Errors)
                .Select(e => e.ErrorMessage)
                .ToList();
            return BadRequest(new { success = false, message = "请求参数验证失败", errors });
        }

        var (success, message) = await _menuService.UpdateAsync(request);

        if (success)
        {
            return Ok(new { success = true, message });
        }
        else
        {
            return BadRequest(new { success = false, message });
        }
    }

    /// <summary>
    /// 删除菜单
    /// </summary>
    /// <param name="id">菜单ID</param>
    /// <returns>删除结果</returns>
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var (success, message) = await _menuService.DeleteAsync(id);

        if (success)
        {
            return Ok(new { success = true, message });
        }
        else
        {
            return BadRequest(new { success = false, message });
        }
    }

    /// <summary>
    /// 获取菜单列表
    /// </summary>
    /// <param name="request">请求参数</param>
    /// <returns>菜单列表</returns>
    [HttpGet]
    public async Task<IActionResult> GetList([FromQuery] GetMenusRequestDto request)
    {
        var (success, message, data) = await _menuService.GetListAsync(request);

        if (success)
        {
            return Ok(new { success = true, message, data });
        }
        else
        {
            return BadRequest(new { success = false, message });
        }
    }

    /// <summary>
    /// 获取菜单详情
    /// </summary>
    /// <param name="id">菜单ID</param>
    /// <returns>菜单详情</returns>
    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var (success, message, data) = await _menuService.GetByIdAsync(id);

        if (success)
        {
            return Ok(new { success = true, message, data });
        }
        else
        {
            return BadRequest(new { success = false, message });
        }
    }

    /// <summary>
    /// 调整菜单顺序
    /// </summary>
    /// <param name="request">调整顺序请求</param>
    /// <returns>调整结果</returns>
    [HttpPut("order")]
    public async Task<IActionResult> UpdateOrder([FromBody] UpdateMenuOrderRequestDto request)
    {
        if (!ModelState.IsValid)
        {
            var errors = ModelState.Values
                .SelectMany(v => v.Errors)
                .Select(e => e.ErrorMessage)
                .ToList();
            return BadRequest(new { success = false, message = "请求参数验证失败", errors });
        }

        var (success, message) = await _menuService.UpdateOrderAsync(request);

        if (success)
        {
            return Ok(new { success = true, message });
        }
        else
        {
            return BadRequest(new { success = false, message });
        }
    }

    /// <summary>
    /// 获取菜单树
    /// </summary>
    /// <returns>菜单树</returns>
    [HttpGet("tree")]
    public async Task<IActionResult> GetMenuTree()
    {
        var (success, message, data) = await _menuService.GetMenuTreeAsync();

        if (success)
        {
            return Ok(new { success = true, message, data });
        }
        else
        {
            return BadRequest(new { success = false, message });
        }
    }
} 